/**
 * @author cwoodard
 */

// master data structure 
var floristsAndCollections = new Array();

function selectFloristAndArrangements(florist, arrangements)
{
	selectFlorist(florist);
	selectArrangements(arrangements);
}

function selectFlorist( florist )
{
	var floristDropDown = document.getElementById('florist');	
	for (var i = 0; i < floristDropDown.length; i++) 
	{
		var florist_name = floristDropDown.options[i].value;
		if(florist == florist_name) 
		{
			floristDropDown.selectedIndex = i;
			floristDropDown.options[i].selected = true;
			loadCollectionsList();
		}
	}
}

function selectArrangements( arrangements )
{
	var collectionDropDown = document.getElementById('collection');
	for( var i=0; i<arrangements.length; i++ )
	{
		var arrangement_name = arrangements[i];
		for( var j=0; j<collectionDropDown.options.length; j++ )
		{
			var parts = collectionDropDown.options[j].value.split(' : ');
			var collection_name = parts[0];
			console.log('comparing "'+arrangement_name+'" to "'+collection_name+'"');
			if(arrangement_name == collection_name)
			{
				collectionDropDown.options[j].selected = true;
				console.log('  matches');
			}
		}
	}	
}

function getFloristsAndCollectionsForBranch( branch_number )
{
	console.log('getting for branch_number '+branch_number);
    dojo.xhrGet( {
        url:'/index/floristsandcollections/branch_number/'+branch_number,
        handleAs: 'json',
		sync:true,
        load: function(florists) {
			floristsAndCollections = florists;
			
			// now load caterers
			var floristDropDown = document.getElementById('florist');
			floristDropDown.length = 0;
			floristDropDown.options[floristDropDown.length] = new Option('Select a florist', 'Select a florist');
			for( var i=0; i < floristsAndCollections.length; i++ )
			{
				var florist = floristsAndCollections[i];	
				var str = florist.name;
				var valstr = ':'+str+':';
				floristDropDown.options[floristDropDown.length] = new Option(str, valstr);
			}
			
			var collectionDropDown = document.getElementById('collection');
			collectionDropDown.length = 0;
			collectionDropDown.options[collectionDropDown.length] = new Option('Select a collection', 'Select a collection');
        }
    } );
}

function updateTotalCost( costValue )
{
	var listTotalField = document.getElementById('list_price_total');
	var adjTotalField = document.getElementById('total');
	listTotalField.value = priceFormatter( costValue );
//	adjTotalField.value = costValue;
}

function collectionChanged()
{
	var floristDropDown = document.getElementById('florist');
	var collectionDropDown = document.getElementById('collection');
	var selectedFloristIdx = floristDropDown.selectedIndex-1;

        if(selectedFloristIdx<0) return;
	var collections = floristsAndCollections[selectedFloristIdx].collections;
	var totalField = document.getElementById('total');
	var collectionsField = document.getElementById('collections');
	var selectionsbox = document.getElementById('selections');
	
	var str = "<collections>";
	totalCost = 0.0;
	selectionsbox.value = '';
	for( var i=0; i < collectionDropDown.length; i++ )
	{
		if(collectionDropDown.options[i].selected)
		{
			str = str + "<collection>";
			str = str + "<name>" + collections[i].name + "</name>";
			str = str + "<price>" + collections[i].price + "</price>";
			totalCost += 1.0*collections[i].price;
			str = str + "</collection>";
			selectionsbox.value += collections[i].name;
			selectionsbox.value += '\n';
			selectionsbox.value += collections[i].description;
			selectionsbox.value += '\n';
			selectionsbox.value += '\n';
		}	
	}
	str = str + "</collections>";
	collectionsField.value = str;
	
	// commafy this one
	//totalField.value = totalCost;
	updateTotalCost(totalCost);
}

function loadCollectionsList()
{
	var floristDropDown = document.getElementById('florist');
	var collectionDropDown = document.getElementById('collection');
	var selectedFloristIdx = floristDropDown.selectedIndex-1;
	var collections = floristsAndCollections[selectedFloristIdx].collections;
		
	// remove the existing menus
	collectionDropDown.length = 0;

//	collectionDropDown.options[collectionDropDown.length] = new Option('Select a collection', 'Select a collection');
	for (var i = 0; i < collections.length; i++) {
		var str = '';
		str = str + collections[i].name + ' : ';
		str = str + collections[i].price;
		collectionDropDown.options[collectionDropDown.length] = new Option(str, str);
	}
}

function updateTotalField()
{
    var newTotalForm = dijit.byId("newTotalForm");
	var newTotalField = dijit.byId('new_total');
	var oldTotalField = document.getElementById('list_price_total');
	oldTotalField.value = priceFormatter2(newTotalField.attr('value'));
}

function showNewTotalForm()
{
    // get form
    var newTotalForm = dijit.byId("newTotalForm");
	var newTotalField = dijit.byId('new_total');
	
    newTotalForm.execute=updateTotalField;

    // make sure contents are empty
    dijit.byId("new_total").attr('value','');

    newTotalForm.show();
}

dojo.addOnLoad( function() { 

	getFloristsAndCollectionsForBranch(current_branch);
	selectFloristAndArrangements(stored_florist,stored_collection);
	collectionChanged();
	// 
} );
